/*
 * PopupMenu.java
 *
 * Created on 2007年9月20日, 下午10:58
 *
 * 继承JPopupMenu 产生动态右键菜单 
 * and open the template in the editor.
 */

package cn.tearcry.jsudoku;
import javax.swing.*;
import java.util.*;
import java.awt.*;
import java.awt.event.*;

/**
 *
 * @author 马建乐
 */
public class PopupMenu extends JPopupMenu{
    
    /** 当前所选按钮候选数表 */
    private ArrayList <Integer> availableNum=new ArrayList <Integer>();
    /** 当前所选按钮 */
    private GridButton button;
    /** 菜单中是否有菜单项*/
    private boolean haveItems;
    
    private GridButtonFactory factory;
    private int currentStep=0;
    
    /**
     * 创建一个右键菜单
     */
    public PopupMenu (GridButton button,GridButton[][] btnArray,GridButtonFactory factory) {
        super();
       
        this.button=button;        
        this.factory=factory;
        availableNum=new AvailableNum(button,btnArray).getAvailableNum ();
        if(availableNum==null) {
            haveItems=false;
            return;
        }
        
        if(availableNum.size ()==1&&availableNum.get (0).intValue ()==button.getNum ()) {
          haveItems=false;
          
      }
        createMenu();
        haveItems=true;
    }
    
    
    public boolean haveItems() {
        return haveItems;
    }
    
    public void setHaveItems(boolean have) {
        haveItems=have;
    }
    
    
    //创建右键菜单项
    private void createMenu() {       
      
        //菜单项   
       JMenuItem[] items=new JMenuItem[availableNum.size ()];
       //创建“添加”项
       if(!button.isUserHasFill()) {
           if(availableNum.size()==0)
               factory.displayWrongInfo();
            for(int i=0;i<items.length;i++) {
                   final int n=availableNum.get(i).intValue ();
                   items[i]=new JMenuItem("添加 "+availableNum.get (i));
                   items[i].addActionListener(new ActionListener() {
                       public void actionPerformed(ActionEvent evt) {
                                move(n,button,true);
                         if(factory.getDisplayCandidates())
                           factory.displayCandidates();
                           
                       }
                   });   
                add(items[i]);     
           }//endfor
        }//endif
       if(factory.getUndoManager().canUndo()) {
          
           JMenuItem item=new JMenuItem("后退");
           item.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent evt) {
                   factory.getUndoManager().undo();
                   factory.closeDisplayGameInfo();
               }
           });
           add(item);
       }
       
       if(factory.getUndoManager().canRedo()) {           
              
           JMenuItem item=new JMenuItem("撤消后退");
           item.addActionListener(new ActionListener() {
               public void actionPerformed(ActionEvent evt) {
                   factory.getUndoManager().redo();
                   factory.closeDisplayGameInfo();
               }
           });
           add(item);
       }    
    }   
    
    private void move(int num,GridButton button,boolean blockWrongMove) {
        
        if(!factory.getBlockWrongMove()||factory.isVaild(num,button.getRow(),button.getColumn())) {
               //开始记录复原信息 
               int[] info=new int[3];
                info[0]=button.getRow();//记录行
                info[1]=button.getColumn();//记录列
                info[2]=num;//记录数字
                factory.getUndoManager().record(info);                
                button.setNum (num);
               
                factory.goOneStep();
                /*
                if(factory.getCurrentStep()==factory.getBlankNumber()) {
                        JOptionPane.showMessageDialog(factory.getFrame(),"恭喜你完成了本道题目","jSudoku",JOptionPane.INFORMATION_MESSAGE);
                        //将游戏状态设置为结束
                        factory.setGameState(factory.END);                      
                        haveItems=false;
                        factory.closeDisplayGameInfo();
                    
                }
                 */
           }//endif

          else {
               Toolkit tool=Toolkit.getDefaultToolkit();
               tool.beep();
               factory.closeDisplayGameInfo();
               factory.displayWrongInfo();
               return;
          }
    }
    
   
}
    

